<template>
    <div class="main">
        <Navi title="发布" back="1" ></Navi>
        <textarea name="" id="" cols="30" rows="10" v-model="text"></textarea>
        <span class="add_title">添加图片<span style="color:#ff0000">(最多添加4张)</span> </span>
        <div class="img_list">
            <div class="add_view" @click="upload()">
                <img :src="require('../assets/add.png')" alt="" srcset="" > 
                
            </div>
            <div class="img_item" v-for="(item, index) in img_list" :key="index" :src="item" v-aspectfill>
                <img :src="require('../../public/img/err.png')" class="del" alt="xx" @click.stop="del_img(index)">
            </div>
        </div>
        <div class="mood_view">
            <span>心情</span>
            <i :class="i<=mood?'iconfont icon-aixin':'iconfont icon-aixin1' " v-for="i in 5" :key='i' @click="change_mood(i)"></i>
        </div>
        <button @click="submit()">发布</button>
        <input type="file" name="" @change="upload_img($event)" ref="file" id="" accept="image/*" multiple style="display:none">
        <Tabbar :indexs="1"></Tabbar>
    </div>
</template>
<script>
import {reactive,ref,getCurrentInstance,defineComponent} from 'vue'
export default {
    setup(){
        const {proxy}=getCurrentInstance()
        let img_list=reactive([])
        let mood=ref(0)
        let text=ref("")
        //点击添加时,打开选择图片
        const upload=()=>{
            proxy.$refs.file.click()
        }
        //上传图片
        const upload_img=(e)=>{
            let files=e.target.files
            console.log(files)
            if(files.length>(4-img_list.length)){
                alert('您只能选择'+(4-img_list.length)+'张图片')
                return false
            }
            [...files].forEach(item => {
                proxy.$ajax.upload(item,res=>{
                    img_list.push(res.url)
                })
            });
        }
        const del_img=(index)=>{
            img_list.splice(index,1)
        }
        const change_mood=(index)=>{
            mood.value=index
        }
        const submit=()=>{
            if(text.value.length<=6){
                alert('文字不能小于6个')
                return
            }
            proxy.$ajax.post('fabu',{
                text:text.value,
                heart:mood.value,
                img_list:img_list.length?img_list.join(","):""
            },res=>{
                img_list.splice(0,img_list.length)
                text.value=""
                mood.value=0
            })
        }
        return {img_list,upload,upload_img,del_img,change_mood,mood,submit,text}
    }
}
</script>
<style lang="less" scoped>
.main{
    margin: 0 30rem;
    font-size: 30rem;
    display: flex;
    flex-direction: column;
    .add_title{
        display: block;
    margin-top: 20rem;
    text-align: left;
    }
    .add_view{
    width: 200rem;
    height: 200rem;
    border: 3px dashed #000;
    border-radius: 20rem;
    img{
        width: 50%;
    height: 50%;
    margin-top: 50rem;
    }
    }
    .img_list{
        display: flex;
        flex-wrap: wrap;
        margin-top: 40rem;
        .img_item{
            width: 200rem;
            height: 200rem;
            margin-left: 20rem;
            position: relative;
            .del{
                position: absolute;
                width: 30rem;
                height: 30rem;
                top: 10rem;
                right: 10rem;
            }
        }
    }
    .mood_view{
        margin-top: 30rem;
    display: flex;
    align-items: center;
    span{
        font-size: 35rem;
    font-weight: 500;
    margin-right: 20rem;
    }
    i{
        font-size: 40rem;
        font-weight: bold;
        margin-right: 10rem;
    }
    }

textarea{
    font-size: 30rem;
    outline: none;
    border: 0;
    background: #f0f0f0;
    width: 690rem;
    height: 400rem;
    margin-top: 30rem;
    border-radius: 16rem;
    padding: 20rem;
}
    button{
        border: 0;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 100rem;
    width: 750rem;
    color: #fff;
    font-size: 35rem;
    font-weight: 600;
    background: #2E7FC1;
    border-radius: 10rem;
}
}
.icon-aixin{
    color: red;
}

</style>